package com.zyh.websocket.controller;

import com.zyh.websocket.entity.Message;
import com.zyh.websocket.service.MessageReceiverService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Controller;

@Slf4j
@Controller
@RequiredArgsConstructor
public class WebSocketController {
    private final MessageReceiverService messageReceiverService;

    @MessageMapping("/chat.send")
    public void handleMessage(
            @Payload Message rawMessage) {

        log.info("收到原始消息：{}", rawMessage);
        if (rawMessage.getSenderId() == null) {
            log.error("消息缺少发送者ID");
            return;
        }

        messageReceiverService.handleMessage(rawMessage, rawMessage.getSenderId());
    }
}
